#include        <stdio.h>
#include        <unistd.h>
#include        <utmp.h>
#include        <fcntl.h>
#include        <time.h>

void showtime(long);
void show_info(struct utmp *);

int main()
{
struct utmp     utbuf;          /* read info into here */
int             utmpfd;         /* read from this descriptor */

if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
perror(UTMP_FILE);
exit(1);
}
while( read(utmpfd, &utbuf, sizeof(utbuf)) == sizeof(utbuf) )
show_info( &utbuf );
close(utmpfd);
return 0;
}
void show_info( struct utmp *utbufp )
{
if ( utbufp->ut_type != USER_PROCESS )
return;

printf("%-8.8s", utbufp->ut_name);      /* the logname  */
printf(" ");                            /* a space      */
printf("%-8.8s", utbufp->ut_line);      /* the tty      */
printf(" ");                            /* a space      */
showtime( utbufp->ut_time );            /* display time */
printf("\n");                          /* newline      */
}
void showtime( long timeval )
{
char    *cp;                    /* to hold address of time      */

cp = ctime(&timeval);           /* convert time to string       */
                                /* string looks like            */			                   /* Mon Feb  4 00:46:40 EST 2016 */
									                                   /* 0123456789012345.            */
									   printf("%12.12s", cp+4 );       /* pick 12 chars from pos 4     */
}
